<?php
namespace app\admin\controller;
use think\Controller;
use think\Session;
use think\Db;
use think\Cache;
use app\admin\model\Menu;

class Login extends Controller
{
    public function index(){
        if(session('appUser')){
            return $this->redirect('/index');
        }
        return $this->fetch();
    }

    /**
     * [check ajax登录校验]
     * @return [type] [description]
     */
    public function check(){
        if(request()->isPost()){
            $username=input('username');
            $pwd=input('password');
            $data['username']=$username;
            $data['password']=$pwd;

            $where['身份证号']=$username;
            $where['登录密码']=$pwd;
            // 用户名和密码匹配检测
            $result = Db::table("教师信息")->where($where)->find();
            if($result){
                $result = findResult($result);
                if($result["离校学期ID"] != 0 && $result["离校学期ID"] != 2) {
                    $msg = "";
                    if($result["离校学期ID"] == 1) $msg = "您已调出，暂不能登录！";
                    //else if($result["离校学期ID"] == 2) $msg = "您已休假，暂不能登录！";
                    else if($result["离校学期ID"] == 3) $msg = "您已离职，暂不能登录！";
                    return $msg;
                }
                unset($result["登录密码"]);
                $result["身份"] = getPositionName($result["职位"]);
                $menu = new Menu();
                $menuList = $menu->getMenu($result["职位"]);
                $result["menuList"] = $menuList;
                Session::set('appUser', $result);
                return "登录成功";
            }else{
                return '用户名或密码不正确！';
                exit;
            }

        }
    }

    // 退出登录
    public function logout(){
        session(null);
        return $this->redirect("/login");
    }



    /**
     * 清空缓存
     */
    public function clear(){
        Cache::clear();
        return '缓存清除成功！';
    }
}
